package com.bosicloud.cmb.ibp.api.manager.util;

import com.alibaba.fastjson.JSONObject;
import com.bosicloud.cmb.ibp.api.manager.commom.model.IbpApiDocParams;
import io.swagger.models.Model;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class ParameterProcessor {

    private final Swagger swagger;
    private final ModelProcessor modelProcessor;

    public ParameterProcessor(Swagger swagger) {
        this.swagger = swagger;
        modelProcessor = new ModelProcessor(swagger);

    }

    public List<IbpApiDocParams> processParameters(List<Parameter> parameters) {

        List<IbpApiDocParams> paramsList = new ArrayList<>();
        if (Objects.isNull(parameters)) {
            return paramsList;
        }

        for (Parameter parameter : parameters) {

            if (parameter instanceof RefParameter) {
                RefParameter refParameter = (RefParameter) parameter;
                Map<String, Model> modelMap = swagger.getDefinitions();
                Model model = modelMap.get(refParameter.get$ref());
                JSONObject jsonObject = modelProcessor.processModel(model);
                paramsList.add(setApiDocParams(jsonObject.toJSONString(), "ref", "object", true, ""));

            }
            if (parameter instanceof BodyParameter) {
                BodyParameter bodyParameter = (BodyParameter) parameter;
                final Model schema = bodyParameter.getSchema();
                JSONObject jsonObject = modelProcessor.processModel(schema);
                paramsList.add(setApiDocParams(jsonObject.toJSONString(), "body", "object", true, ""));
            }
            if (parameter instanceof PathParameter) {
                PathParameter pathParameter = (PathParameter) parameter;
                paramsList.add(setApiDocParams(pathParameter.getName(), pathParameter.getIn(),
                        pathParameter.getType(), pathParameter.getRequired(), pathParameter.getDescription()));
            }

            if (parameter instanceof QueryParameter) {
                QueryParameter queryParameter = (QueryParameter) parameter;
                paramsList.add(setApiDocParams(queryParameter.getName(), queryParameter.getIn(),
                        queryParameter.getType(), queryParameter.getRequired(),
                        queryParameter.getDescription()));

            }

            if (parameter instanceof HeaderParameter) {
                HeaderParameter headerParameter = (HeaderParameter) parameter;
                paramsList.add(setApiDocParams(headerParameter.getName(), headerParameter.getIn(),
                        headerParameter.getType(), headerParameter.getRequired(),
                        headerParameter.getDescription()));
            }
            if (parameter instanceof FormParameter) {
                FormParameter formParameter = (FormParameter) parameter;
                paramsList.add(setApiDocParams(formParameter.getName(), formParameter.getIn(),
                        formParameter.getType(), formParameter.getRequired(),
                        formParameter.getDescription()));
            }
        }
        return paramsList;
    }

    private IbpApiDocParams setApiDocParams(String name, String paramIn, String paramType, Boolean required, String desp) {
        IbpApiDocParams ibpApiDocParams = new IbpApiDocParams();
        ibpApiDocParams.setName(name);
        ibpApiDocParams.setParamIn(paramIn);
        ibpApiDocParams.setParamType(paramType);
        ibpApiDocParams.setRequired(required ? "Y" : "N");
        ibpApiDocParams.setDesp(desp);
        return ibpApiDocParams;
    }
}
